package cn.hzc.fortess.core.interceptor;


import cn.hzc.fortess.core.common.HttpContext;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpSession;


@Aspect
@Component
public class SessionHolderInterceptor {

    @Pointcut("execution(* cn.hzc.fortess.system.controller.*.*(..))")
    public void cutService() {
    }

    @Around("cutService()")
    public Object sessionKit(ProceedingJoinPoint point) throws Throwable {
        HttpSessionContext.put(HttpContext.getRequest().getSession());
        try {
            return point.proceed();
        } finally {
            HttpSessionContext.remove();
        }
    }


    private static class HttpSessionContext {

        private final static ThreadLocal<HttpSession> tl = new ThreadLocal<HttpSession>();

        public static void put(HttpSession s) {
            tl.set(s);
        }

        public static HttpSession get() {
            return tl.get();
        }

        public static void remove() {
            tl.remove();
        }
    }


}





